<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

// 应用公共文件
define('SUCCESS', 0);
define('NULL_PARAM', 10010);
define('ERRORS', 10500);
define('ERRORS_METHOD', 10012);
define('ERRORS_PARAM', 10013);
define('FAILED', 10014);
define('FAILED_AUTHENTICATE', 10015);
define('NULL_DATA', 10016);
define('HAS_VOSTED', 10017);
define('CODE_ERROR', 10019);
define('DATA_EXIST', 10020);
define('EMAIL_NOT_EXIST', 10021);
define('ERRORS_PASS', 10022);
define('LOGIN_OUT', 10023);
define('NOT_ACTIVAT', 10024);
define('LOGIN_FAILED', 10025);
define('OLDPASS_ERROR', 10026);
define('FINDPASS_FAILED', 10027);
define('USERNAME_LENGTH', 10028);
define('EMAIL_FORMAT_ERROR', 10029);
define('CANT_MODIFY_DEPART', 10030);
define('NO_CHINESE', 10031);
define('TEXT_TOO_LENGTH', 10032);
define('NULL_WHERE', 10033);
define('TABLE_NOT_FOUND', 10034);
define('TOKEN_MISS', 10035);
define('NO_ACCESS', 10036);
define('NOT_CHECK', 10037);
define('MISS_PARAM', 10038);
define('CAPTCHA_ERROR', 10039);
define('NULL_FILE', 10040);
define('INSUFFICIENT_BALANCE', 10041);
define('FILE_PARSE_FAILED', 10042);
define('SECRET_ERROR', 10043);
define('NOT_REGISTER', 10044);
define('ROUTE_MISS', 10045);

define('RPC_EOL', "\r\n\r\n");
//think\facade\Config::setYaconf('foo');

/**
 * 格式化返回数据
 * @param null $data
 * @param int $errcode
 * @param string $errmsg
 * @return array
 */
function format($data=null,$errcode=SUCCESS,$errmsg=''): array
{
    if(is_null($data)) {
        $data = '';
    }

    return [
        'errcode'    => $errcode,
        'errmsg'     => $errmsg?:lang((string)$errcode),
        'data'       => $data,
    ];
}

/**
 * rpc服务请求
 *
 * @param $host
 * @param $class
 * @param $method
 * @param $param
 * @param string $version
 * @param array $ext
 * @return array
 * @throws Exception
 */
function rpc_request($host, $class, $method, $param, $version = '1.0', $ext = []): array
{
    $fp = stream_socket_client($host, $errno, $errstr);
    if (!$fp) {
        throw new Exception("stream_socket_client fail errno={$errno} errstr={$errstr}");
    }

    $req = [
        "jsonrpc" => '2.0',
        "method" => sprintf("%s::%s::%s", $version, $class, $method),
        'params' => $param,
        'id' => '',
        'ext' => $ext,
    ];
    $data = json_encode($req) . RPC_EOL;
    fwrite($fp, $data);

    $result = '';
    while (!feof($fp)) {
        $tmp = stream_socket_recvfrom($fp, 1024);

        if ($pos = strpos($tmp, RPC_EOL)) {
            $result .= substr($tmp, 0, $pos);
            break;
        } else {
            $result .= $tmp;
        }
    }

    fclose($fp);
    return json_decode($result, true);
}